package com.yhp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.comm.util.JsonUtils;
import com.system.common.pojo.BisShopPost;
import com.system.common.pojo.SysMenu;
import com.yhp.service.BBisShopPostService;

@Controller
@RequestMapping("bsp")
public class BBisShopPostController {
	@Autowired
	private BBisShopPostService postService;

	/**
	 * 综合查询
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("post")
	public String post(Model model, HttpServletRequest request) {
		Map<String, Object> map = initMap(request);
		List<BisShopPost> bisShopPostList = postService.selectByExample(map);
		model.addAttribute("bisShopPostList", bisShopPostList);
		return "bis_shop/post";
	}

	public Map<String, Object> initMap(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String postCode = request.getParameter("postCode");
		String postName = request.getParameter("postName");
		if (postCode != null) {
			map.put("postCode", postCode);
		}
		if (postName != null) {
			map.put("postName", postName);
		}
		return map;
	}

	/**
	 * 添加
	 * 
	 * @param bisShopPost
	 * @return
	 */
	@RequestMapping("post_add")
	public String post_add(BisShopPost bisShopPost,String menuValue,HttpSession session) {
		postService.insert(bisShopPost,menuValue,session);
		return "redirect:/bsp/post";
	}

	/**
	 * 修改
	 * 
	 * @param bisShopPost
	 * @return
	 */
	@RequestMapping("post_update")
	public String post_update(BisShopPost bisShopPost,String menuValue,HttpSession session) {
		postService.updateByPrimaryKey(bisShopPost,menuValue,session);
		return "redirect:/bsp/post";
	}

	/**
	 * 删除
	 * 
	 * @param bisShopPost
	 * @return
	 */
	@RequestMapping("post_del")
	public String post_del(String postId,String postCode) {
		postId = postId.substring(0, postId.length() - 1);
		postCode = postCode.substring(0, postCode.length() - 1);
		postService.deleteByPrimaryKey(postId,postCode);
		return "redirect:/bsp/post";
	}

	@RequestMapping(value = "queryMenus", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String queryMenus() {
		List<SysMenu> menus = postService.queryMenus();
		SysMenu m = new SysMenu();
		m.setMenuId("0");
		m.setCascadeId("0");
		m.setMenuName("功能菜单");
		m.setMenuType("2");
		m.setParentId("-1");
		m.setIsAutoExpand("1");
		m.setStatus("1");
		m.setEditMode("1");
		m.setSortNo(2);
		menus.add(m);
		String jsonMenus = JsonUtils.toJson(menus);
		return jsonMenus;
	}

	@RequestMapping(value = "menusCheck", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String menusCheck(String postId) {
		boolean state = true;
		List<SysMenu> menus = postService.menusCheck(postId);
		SysMenu m = new SysMenu();
		m.setMenuId("0");
		m.setCascadeId("0");
		m.setMenuName("功能菜单");
		m.setMenuType("2");
		m.setParentId("-1");
		m.setIsAutoExpand("1");
		m.setStatus("1");
		m.setEditMode("1");
		m.setSortNo(2);
		for (SysMenu sysMenu : menus) {
			if (!sysMenu.isChecked()) {
				state = false;
			}
		}
		if (state) {
			m.setChecked(true);
		}
		menus.add(m);
		String jsonMenus = JsonUtils.toJson(menus);
		return jsonMenus;
	}
	@RequestMapping(value="checkpoatCode",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String checkpoatCode(String postCode){
		String msg=postService.checkpoatCode(postCode);
		return msg;
		
	}
}
